package com.stars.services.impl.system;/**
 * Created by luodajie on 2019/1/5.
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.signature.MD5Util;
import com.stars.common.valid.MyException;
import com.stars.dao.mapper.system.SystemUserMapper;
import com.stars.entity.ListPage;
import com.stars.entity.dto.sys.SystemUserAppOrgDTO;
import com.stars.entity.dto.sys.SystemUserDTO;
import com.stars.entity.dto.system.SysRoleDTO;
import com.stars.entity.sys.SystemUser;
import com.stars.services.impl.BaseAccessServiceImpl;
import com.stars.services.system.UserAccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :dj
 * @version : 1.0.0
 * @description
 * @Date 2019-01-05 12:43
 * @note
 */
@Service
public class UserAccessServiceImpl extends BaseAccessServiceImpl<SystemUserMapper, SystemUser> implements UserAccessService {
    @Autowired
    private SystemUserMapper systemUserMapper;

    @Override
    public SystemUserAppOrgDTO authUser(String username, String password) throws Exception {
        //根据用户名查询是否存在该用户
        SystemUserAppOrgDTO user = systemUserMapper.findByName(username);
        //使用传入的密码进行加密。匹配是否对应得上数据库的用户密码。
        if (null == user) {
            throw new MyException("账号或密码错误，请重试！");
        } else if (1 == user.getIsLock()) {
            throw new MyException("当前账号已经被锁定，请联系管理员");
        } else if (1 == user.getUserEnabled()) {
            throw new MyException("当前账号不可用，请联系管理员");
        }

        String sign = MD5Util.sign(password, false, user.getUserSalt(), user.getUserSalt2());
        if (user.getUserPassword().equals(sign)) {
            //如果匹配，则表示帐号密码正确，登录成功
            return user;
        } else {
            //否则登录失败
            throw new MyException("账号或密码错误，请重试！");
        }
    }

    @Override
    public SystemUserAppOrgDTO userQueryById(String id) throws Exception {
        return this.findById(id);
    }

    @Override
    public SystemUserAppOrgDTO findById(String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        return systemUserMapper.findByUserId(id);
    }

    @Override
    public PageInfo<SystemUserDTO> findByPage(Integer pageNum, Integer pageSize, SystemUserDTO command) throws Exception {
        ListPage<SystemUserDTO> listPage = new ListPage<>(pageNum, pageSize, command);
        PageHelper.startPage(pageNum, pageSize);
        List<SystemUserAppOrgDTO> blogList = systemUserMapper.findByPage(listPage);
        List<SystemUserDTO> systemUserDTOList = new ArrayList<>();
        for (SystemUserAppOrgDTO s : blogList) {
            SystemUserDTO userDTO = new SystemUserDTO();
            ObjectCopyUtils.copyPropertiesIf(s, userDTO);
            List<String> roleIdList = new ArrayList<>();
            List<String> roleNameList = new ArrayList<>();
            for (SysRoleDTO systemUserRole : s.getSysRoleDTOList()) {
                if (null == systemUserRole) {
                    continue;
                }
                roleIdList.add(systemUserRole.getId());
                roleNameList.add(systemUserRole.getRoleName());
            }
            userDTO.setRoleIds(roleIdList);
            userDTO.setRoleName(roleNameList);
            systemUserDTOList.add(userDTO);
        }
        PageInfo<SystemUserDTO> pageInfo = new PageInfo<>(systemUserDTOList);
        return pageInfo;
    }

    @Override
    public PageInfo<SystemUserAppOrgDTO> findByRolePage(Integer pageNum, Integer pageSize, SystemUserDTO command) throws Exception {
        //没有没有角色id不查询
        if (org.apache.commons.lang3.StringUtils.isBlank(command.getRoleId())) {
            return new PageInfo<>();
        }
        ListPage<SystemUserDTO> listPage = new ListPage<>(pageNum, pageSize, command);
        PageHelper.startPage(pageNum, pageSize);
        List<SystemUserAppOrgDTO> blogList = systemUserMapper.findByRoleIdPage(listPage);
        PageInfo<SystemUserAppOrgDTO> pageInfo = new PageInfo<>(blogList);
        return pageInfo;
    }
}
